package com.lsfb.signsys.view.BottomNavView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.lsfb.signsys.utils.DensityUtils;

import java.lang.reflect.Field;

/**
 * Created by zgscwjm on 2018/3/28.
 */

public class BottomNavigationItemBadgeView extends BottomNavigationItemView implements IBadge {

    private BadgeView mBadgeView;

    private int mBadgeDefaultMargin;

    private int mBadgeShiftAmount;

    public BottomNavigationItemBadgeView(@NonNull Context context) {
        this(context, null);
    }

    public BottomNavigationItemBadgeView(@NonNull Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("RestrictedApi")
    public BottomNavigationItemBadgeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mBadgeView = new BadgeView(context);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
        layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        mBadgeDefaultMargin = DensityUtils.dp2px(context, 12);
        layoutParams.setMarginStart(mBadgeDefaultMargin);
        mBadgeView.setLayoutParams(layoutParams);
        addView(mBadgeView);


        try {
            Field field = null;
            field = this.getClass().getSuperclass().getDeclaredField("mShiftAmount");
            field.setAccessible(true);
            mBadgeShiftAmount = field.getInt(this);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void incrCount() {
        mBadgeView.incrCount();
    }

    @Override
    public void decrCount() {
        mBadgeView.decrCount();
    }

    @Override
    public void setBadgeMole(int model) {
        mBadgeView.setBadgeMole(model);
    }

    @Override
    public void setCounter(int count) {
        mBadgeView.setCounter(count);
    }

    @Override
    public void BadgeVisibility(boolean visibility) {
        mBadgeView.BadgeVisibility(visibility);
    }


    @SuppressLint("RestrictedApi")
    @Override
    public void setChecked(boolean checked) {

        //如果badge是显示的状态
        if (mBadgeView.isShown()) {

            //如果是滑动缩放模式
            if (false) {
                if (checked) {
                    LayoutParams badgeParams = (LayoutParams) mBadgeView.getLayoutParams();
                    badgeParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
                    badgeParams.topMargin = 0;
                    badgeParams.bottomMargin = 0;
                    badgeParams.leftMargin = mBadgeDefaultMargin;
                    mBadgeView.setLayoutParams(badgeParams);
                } else {
                    LayoutParams badgeParams = (LayoutParams) mBadgeView.getLayoutParams();
                    badgeParams.gravity = Gravity.CENTER;
                    badgeParams.topMargin = 0;
                    badgeParams.bottomMargin = mBadgeDefaultMargin / 2;
                    badgeParams.leftMargin = mBadgeDefaultMargin;
                    mBadgeView.setLayoutParams(badgeParams);
                }
            } else {
                //这里是 有个弹动缩放 效果(果冻)
                if (checked) {
                    LayoutParams badgeParams = (LayoutParams) mBadgeView.getLayoutParams();
                    badgeParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
                    badgeParams.topMargin = mBadgeShiftAmount;
                    badgeParams.bottomMargin = 0;
                    badgeParams.leftMargin = mBadgeDefaultMargin;
                    mBadgeView.setLayoutParams(badgeParams);
                } else {
                    LayoutParams badgeParams = (LayoutParams) mBadgeView.getLayoutParams();
                    badgeParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
                    badgeParams.topMargin = 0;
                    badgeParams.bottomMargin = 0;
                    badgeParams.leftMargin = mBadgeDefaultMargin;
                    mBadgeView.setLayoutParams(badgeParams);

                }
            }
        }

        super.setChecked(checked);


    }
}
